<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


abstract class MModel 
    extends MComponent 
    implements IteratorAggregate
{
                private $_errors = array();
    
    
    
    public function hasErrors($attribute = null)
    {
        if ($attribute === null)
            return $this->_errors !== array();
        else
            return isset($this->_errors[$attribute]);
    }

    
    public function getErrors($attribute = null)
    {
        if ($attribute === null)
            return $this->_errors;
        
        if (isset($this->_errors[$attribute]))
            return $this->_errors[$attribute]; 

        return array();
    }

    
    public function getError($attribute)
    {
        if (isset($this->_errors[$attribute]))
            return $this->_errors[$attribute]; 

        return null;
    }

    
    public function addError($attribute, $error)
    {
        $this->_errors[$attribute][] = $error;
    }

    
    public function addErrors($errors)
    {
        foreach ($errors as $attribute=>$error)
        {
            if (is_array($error))
            {
                foreach ($error as $e)
                    $this->_errors[$attribute][] = $e;
            }
            else
                $this->_errors[$attribute][] = $error;
        }
    }

    
    public function clearErrors($attribute = null)
    {
        if ($attribute === null)
            $this->_errors = array();
        else
            unset($this->_errors[$attribute]);
    }
    
    
    public function getIterator()
    {
        return new ArrayIterator($this->_m);
    }

    
    public function toJson()
    {
        if (is_array($this->_m) === false ||
            count($this->_m) <= 0)
        {
            return false;
        }
        
        $json = json_encode($this->_m);
        return $json;
    }
}
?>